Telegram Group & Telegram Channel
🎯 Как настроить Spring Security с кастомной формой логина

Безопасность — базовая вещь. Spring Security по умолчанию кидает вас в дефолтную форму логина. Но в реальности — нужна своя, кастомная.

1️⃣ Добавьте зависимость
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>


2️⃣ Создайте SecurityConfig
@Configuration
@EnableWebSecurity
public class SecurityConfig {

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(auth -> auth
.requestMatchers("/login", "/css/**").permitAll()
.anyRequest().authenticated()
)
.formLogin(form -> form
.loginPage("/login")
.defaultSuccessUrl("/dashboard", true)
.permitAll()
)
.logout(logout -> logout
.logoutSuccessUrl("/login?logout")
);

return http.build();
}

@Bean
public UserDetailsService userDetailsService() {
var user = User.withUsername("admin")
.password("{noop}admin123") // Для простоты, без шифрования
.roles("ADMIN")
.build();
return new InMemoryUserDetailsManager(user);
}
}


3️⃣ Создайте кастомную HTML-форму

src/main/resources/templates/login.html (если используете Thymeleaf):
<!DOCTYPE html>
<html>
<head><title>Login</title></head>
<body>
<h2>Login</h2>
<form method="post" action="/login">
<label>Username: <input type="text" name="username" /></label><br/>
<label>Password: <input type="password" name="password" /></label><br/>
<button type="submit">Login</button>
</form>
</body>
</html>


4️⃣ Реализуете контроллер
@RestController
public class DashboardController {

@GetMapping("/dashboard")
public String dashboard() {
return "Welcome to the dashboard!";
}
}


🐸 Библиотека джависта #буст
Please open Telegram to view this post
VIEW IN TELEGRAM



tg-me.com/javaproglib/6659
Create:
Last Update:

🎯 Как настроить Spring Security с кастомной формой логина

Безопасность — базовая вещь. Spring Security по умолчанию кидает вас в дефолтную форму логина. Но в реальности — нужна своя, кастомная.

1️⃣ Добавьте зависимость

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>


2️⃣ Создайте SecurityConfig
@Configuration
@EnableWebSecurity
public class SecurityConfig {

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(auth -> auth
.requestMatchers("/login", "/css/**").permitAll()
.anyRequest().authenticated()
)
.formLogin(form -> form
.loginPage("/login")
.defaultSuccessUrl("/dashboard", true)
.permitAll()
)
.logout(logout -> logout
.logoutSuccessUrl("/login?logout")
);

return http.build();
}

@Bean
public UserDetailsService userDetailsService() {
var user = User.withUsername("admin")
.password("{noop}admin123") // Для простоты, без шифрования
.roles("ADMIN")
.build();
return new InMemoryUserDetailsManager(user);
}
}


3️⃣ Создайте кастомную HTML-форму

src/main/resources/templates/login.html (если используете Thymeleaf):
<!DOCTYPE html>
<html>
<head><title>Login</title></head>
<body>
<h2>Login</h2>
<form method="post" action="/login">
<label>Username: <input type="text" name="username" /></label><br/>
<label>Password: <input type="password" name="password" /></label><br/>
<button type="submit">Login</button>
</form>
</body>
</html>


4️⃣ Реализуете контроллер
@RestController
public class DashboardController {

@GetMapping("/dashboard")
public String dashboard() {
return "Welcome to the dashboard!";
}
}


🐸 Библиотека джависта #буст

BY Библиотека джависта | Java, Spring, Maven, Hibernate




Share with your friend now:
tg-me.com/javaproglib/6659

View MORE
Open in Telegram


Библиотека джависта | Java Spring Maven Hibernate Telegram | DID YOU KNOW?

Date: |

For some time, Mr. Durov and a few dozen staffers had no fixed headquarters, but rather traveled the world, setting up shop in one city after another, he told the Journal in 2016. The company now has its operational base in Dubai, though it says it doesn’t keep servers there.Mr. Durov maintains a yearslong friendship from his VK days with actor and tech investor Jared Leto, with whom he shares an ascetic lifestyle that eschews meat and alcohol.

Telegram has exploded as a hub for cybercriminals looking to buy, sell and share stolen data and hacking tools, new research shows, as the messaging app emerges as an alternative to the dark web.An investigation by cyber intelligence group Cyberint, together with the Financial Times, found a ballooning network of hackers sharing data leaks on the popular messaging platform, sometimes in channels with tens of thousands of subscribers, lured by its ease of use and light-touch moderation.Библиотека джависта | Java Spring Maven Hibernate from nl


Telegram Библиотека джависта | Java, Spring, Maven, Hibernate
FROM USA